﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.Profile;
using System.Net.Mail;

namespace Continental.UI.Member
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        public String theEmail;
        public String thePasswordForResetUrl;
        public String thePasswordForReset;
        public DateTime theTime;
        public String message;

        protected void Page_Load(object sender, EventArgs e)
        {
            theEmail = Request.QueryString["Email"].ToString();
            thePasswordForResetUrl = Request.QueryString["PasswordForReset"].ToString();

            MembershipUser theUser = Membership.GetUser(theEmail);
            String theUrl = "";
            if (theUser != null)
            {
                ProfileBase theProfile = ProfileBase.Create(theUser.UserName);
                thePasswordForReset = theProfile.GetPropertyValue("PasswordForReset").ToString();
                theTime = DateTime.Parse(theProfile.GetPropertyValue("Time").ToString());
                if (thePasswordForResetUrl == thePasswordForReset && thePasswordForReset != "")
                {
                    if (theTime < DateTime.Now)
                    {
                        theUrl = "~/Thanh-Vien/Thời gian giới hạn reset password đã hết, Bạn hãy gửi lại yêu cầu Reset Password/dang-nhap.aspx";
                        Response.Redirect(theUrl);
                    }
                    else
                    {
                        String newPassword = theUser.ResetPassword();
                        String adminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
                        String password = ConfigurationManager.AppSettings["Password"].ToString();
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                        //thiet lap cac thong so cho viec reset
                        theProfile.SetPropertyValue("PasswordForReset", "");
                        theProfile.SetPropertyValue("Time", DateTime.Now.AddDays(-1));
                        theProfile.Save();

                        // thiet lap email
                        mail.From = new MailAddress(adminEmail);
                        mail.To.Add(theEmail.Trim());
                        mail.Subject = "Test Mail";
                        mail.Body += "Email : " + theEmail + "<br />";
                        mail.Body += "Password : " + newPassword;
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(adminEmail, password);
                        SmtpServer.EnableSsl = true;

                        SmtpServer.Send(mail);
                        message += "Thông tin tài khoản đã được gửi đến email của bạn";
                    }
                }
                else
                {
                    message += "Chuỗi url reset không hợp lệ";
                }
            }

        }
    }
}
